//
//  RequestApi+Login.m
//  MexicoLoan
//
//  Created by wxp on 2025/2/26.
//

#import "RequestApi+Login.h"

@implementation RequestApi (Login)

#pragma mark - 发送验证码的次数
+ (void)requestSendSmsCountWithPhone:(NSString *)phone
                            delegate:(id <RequestDelegate>)delegate
                             success:(void (^)(NSDictionary * response, id mark))success
                             failure:(void (^)(NSString * errorStr, id mark))failure{
    NSDictionary * dic = @{
        KEY_phone: readStr(phone),
        KEY_type:  @"login"};    
    [RequestApi postUrl:[NSString stringWithFormat:@"%@%@",URL_HEAD,SEND_SMS_COUNT] delegate:delegate parameters:dic success:success failure:failure];
}

#pragma mark - 发送验证码
+ (void)requestSendSmsWithPhone:(NSString *)phone
                           type:(NSString *)type
                     smsChannel:(NSString *)smsChannel
                           delegate:(id <RequestDelegate> _Nullable)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    NSDictionary * dic = @{
        KEY_adid:  [GlobalMethod getAdid],
        KEY_phone: readStr(phone)
    };
    [RequestApi postUrl:[NSString stringWithFormat:@"%@%@/%@/%@",URL_HEAD,SEND_SMS_MAPPING,type,smsChannel] delegate:delegate parameters:dic success:success failure:failure];
}

#pragma mark - 是否需要登录活体
+ (void)requestNeedLoginAuthWithPhone:(NSString *)phone
                          vcode:(NSString *)vcode
                           delegate:(id <RequestDelegate> _Nullable)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    [RequestApi postUrl:[NSString stringWithFormat:@"%@%@?%@=%@&%@=%@",URL_HEAD,USER_NEEDLOGINAUTH,KEY_phone,phone,KEY_vcode,vcode] delegate:delegate parameters:nil success:success failure:failure];
}

#pragma mark - 登录
+ (void)requestLoginWithPhone:(NSString *)phone
                        vcode:(NSString *)vcode
             livenChannelEnum:(NSString *)livenChannelEnum
                           delegate:(id <RequestDelegate>)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    NSString *vendorIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSDictionary * dic = @{
        KEY_loginName: readStr(phone),
        KEY_vcode:     readStr(vcode),
        KEY_deviceNo:  vendorIdentifier,
    };
    [RequestApi postUrl:[NSString stringWithFormat:@"%@%@%@",URL_HEAD, USER_LOGIN, livenChannelEnum] delegate:delegate parameters:dic success:^(NSDictionary *response, id mark) {
        //本地存储用户信息
        [GlobalData sharedInstance].Global_Token = readStr(response[KEY_token]);
        [GlobalData sharedInstance].Global_UserCode = [NSString stringWithFormat:@"%@", readStr(response[KEY_userId])];
        
        NSLog(@" --- id --- :%@",readStr(response[KEY_userId]));
        NSLog(@" --- Global_UserCode --- :%@",[GlobalData sharedInstance].Global_UserCode);
        
        // 打点KEY_userId
        BOOL isRegister = [response boolValueForKey:KEY_register];
        if(isRegister){
            [GlobalMethod hitPointWithPageName:KEY_Register action:KEY_Register_Save extInfo:nil];
//            [GlobalMethod addADJEventWithToken:KEY_Register_Event phone:phone userId:[GlobalData sharedInstance].GB_UserID];
        }else{
            [GlobalMethod hitPointWithPageName:KEY_Register action:KEY_Login_Save extInfo:nil];
//            [GlobalMethod addADJEventWithToken:KEY_Login_Event phone:phone userId:[GlobalData sharedInstance].GB_UserID];
        }
        
        if (success != nil) {
            success(response,mark);
        }
        
    } failure:failure];
}

#pragma mark - 退出登录
+ (void)requestLogoutWithDelegate:(id <RequestDelegate> _Nullable)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    [RequestApi postUrl:[NSString stringWithFormat:@"%@%@",URL_HEAD,LOGIN_OUT] delegate:delegate parameters:nil success:success failure:failure];
}

#pragma mark - 活体认证之后的登录
+ (void)requestLiveSuccessLoginWithPhone:(NSString *)phone
                        vcode:(NSString *)vcode
                                 liveImg:(NSString *)liveImg
                              livenessId:(NSString *)livenessId
             livenChannelEnum:(NSString *)livenChannelEnum
                           delegate:(id <RequestDelegate>)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    NSString *vendorIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSDictionary * dic = @{

        KEY_loginName: readStr(phone),
        KEY_vcode: readStr(vcode),
        KEY_liveImg: readStr(liveImg),
        KEY_livenessId: readStr(livenessId),
        KEY_deviceNo: vendorIdentifier,
    };
    [RequestApi postUrl:[NSString stringWithFormat:@"%@%@%@",URL_HEAD, USER_ANTI_FRAUD_LOGIN_SMS, livenChannelEnum] delegate:delegate parameters:dic success:^(NSDictionary *response, id mark) {
        
        //本地存储用户信息
        [GlobalData sharedInstance].Global_Token = readStr(response[KEY_token]);
        [GlobalData sharedInstance].Global_UserCode = [NSString stringWithFormat:@"%@",response[KEY_userId]];
        
        // 打点
        BOOL isRegister = [response boolValueForKey:KEY_register];
        if(isRegister){
            [GlobalMethod hitPointWithPageName:KEY_Register action:KEY_Register_Save extInfo:nil];
        }else{
            [GlobalMethod hitPointWithPageName:KEY_Register action:KEY_Login_Save extInfo:nil];
        }
        
        //登录成功-请求用户个人信息
        [RequestApi requestUserInformationWithDelegate:delegate success:success failure:failure];
        
    } failure:failure];
}

@end
